package com.epoint.clubmanagement.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.epoint.clubmanagement.common.ResultMapUtil;
import com.epoint.clubmanagement.entity.ClubType;
import com.epoint.clubmanagement.service.IClubTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.List;

/**
 * Title: ClubTypeController
 * Description: 俱乐部类型相关的controller
 *
 * @author sqn
 * @version 1.0.0
 * @date 2021/3/17 0017 下午 12:23
 */
@Validated
@Controller
@RequestMapping(value = "/clubType")
public class ClubTypeController {

    @Autowired
    private IClubTypeService clubTypeService;

    /**
     * 转向俱乐部类型页面
     */
    @RequestMapping
    public String clubType() {
        return "/clubType";
    }

    /**
     * 分页查询俱乐部类型列表
     */
    @RequestMapping(value = "/clubTypeQueryPage")
    @ResponseBody
    public Object clubTypeQueryPage(String param, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize) {
        try {
            IPage<ClubType> iPage = clubTypeService.selectClubTypePage(pageNum, pageSize, param);
            return ResultMapUtil.getHashMapMysqlPage(iPage);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 转向俱乐部类型新增页面
     */
    @RequestMapping(value = "/clubTypePage")
    public String clubTypePage() {
        return "/clubTypePage";
    }

    /**
     * 添加一个俱乐部类型
     */
    @RequestMapping(value = "/clubTypeAdd")
    @ResponseBody
    public Object clubTypeAdd(@Valid ClubType clubType) {
        try {
            int i = clubTypeService.addClubType(clubType);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 转向俱乐部类型编辑页面
     */
    @RequestMapping(value = "/clubTypeQueryById")
    public String clubTypeQueryById(@RequestParam(name = "id", required = true) Integer id, Model model) {
        ClubType clubType = clubTypeService.queryClubTypeById(id);
        model.addAttribute("obj", clubType);
        return "/clubTypePage";
    }

    /**
     * 修改一个俱乐部类型
     */
    @RequestMapping(value = "/clubTypeEdit")
    @ResponseBody
    public Object clubTypeEdit(@Valid ClubType clubType) {
        try {
            int i = clubTypeService.editClubType(clubType);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 删除一个俱乐部类型
     */
    @RequestMapping(value = "/clubTypeDelById")
    @ResponseBody
    public Object clubTypeDelById(@RequestParam(name = "id", required = true) Integer id) {
        try {
            int i = clubTypeService.delClubTypeById(id);
            return ResultMapUtil.getHashMapDel(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 获取所有俱乐部类型
     */
    @RequestMapping(value = "/clubTypeList")
    @ResponseBody
    public Object clubTypeList() {
        List<ClubType> clubTypeList = clubTypeService.queryClubTypeList();
        return ResultMapUtil.getHashMapList(clubTypeList);
    }

}
